from django.contrib.auth.models import Permission, Group
from apps.users.models import User
from django.contrib.contenttypes.models import ContentType
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from apps.meiduo_admin.serializer.admin import PermissionSerializer, ContentTypeSerliazer, GroupSerializer, \
    AdminSerializer
from apps.meiduo_admin.utils import PageNum


class PermissionView(ModelViewSet):
    permission_classes = [IsAdminUser]
    pagination_class = PageNum
    queryset = Permission.objects.all().order_by('name')
    serializer_class = PermissionSerializer

    def content_type(self, request):
        content_type = ContentType.objects.all()
        ser = ContentTypeSerliazer(content_type, many=True)
        return Response(ser.data)


class GroupView(ModelViewSet):
    permission_classes = [IsAdminUser]
    pagination_class = PageNum
    queryset = Group.objects.all().order_by('name')
    serializer_class = GroupSerializer

    def simple(self, request):
        pers = Permission.objects.all()
        ser = PermissionSerializer(pers, many=True)
        return Response(ser.data)


class AdminView(ModelViewSet):

    permission_classes = [IsAdminUser]
    pagination_class = PageNum
    queryset = User.objects.filter(is_staff=True).order_by('id')
    serializer_class = AdminSerializer

    def simple(self, request):

        group = Group.objects.all().order_by('id')
        ser = GroupSerializer(group, many=True)
        return Response(ser.data)
